import sideNavTpl from './sideNav.html';
import './sideNav.scss';
/*eslint-disable */
import jianliMenuList from '!json-loader!./jianli.json';
import xiangmubuMenuList from '!json-loader!./xiangmubu.json';
import zhihuibuMenuList from '!json-loader!./zhihuibu.json';
/*eslint-enable */
import storageApi from '../../core/storage/storage';

function getPermittedMenuList(recursionMenuList, permittedMenuList) {
  recursionMenuList.forEach((menu) => {
    menu.isPermitted = false;
    permittedMenuList.forEach((permittedMenu) => {
      if (permittedMenu.name === menu.name) {
        menu.isPermitted = true;
      }
    });

    if (menu.children && menu.children.length) {
      getPermittedMenuList(menu.children, permittedMenuList);
    }
  });
}

function sideNav() {
  'ngInject';

  return {
    restrict: 'E',
    replace: true,
    templateUrl: sideNavTpl,
    scope: {},
    controller($rootScope, $scope, $state, $stateParams, $http, globalService) {
      'ngInject';

      this.view = {
        menuList: [],
      };

      this.source = {
        zhihuibuMenuList,
        xiangmubuMenuList,
        jianliMenuList,
      };

      let orgId = $stateParams.orgId;
      if ($stateParams.orgId) {
        orgId = $stateParams.orgId;
      } else {
        orgId = storageApi.get('orgId');
      }

      $http
        .get(`/api/v1/menu/getMenuForTargetOrg/${orgId}`)
        .then((res) => {
          if (res.success) {
            const orgType = res.data.orgType;
            const permittedMenuList = res.data.list;
            let sourceMenuList = [];
            if (orgType === '1') {
              sourceMenuList = zhihuibuMenuList;
            } else if (orgType === '2') {
              sourceMenuList = jianliMenuList;
            } else if (orgType === '3') {
              sourceMenuList = xiangmubuMenuList;
            }
            this.fatherMenu = res.data.orgTypeName;
            $rootScope.orgType = res.data.orgType;
            storageApi.set('orgType', $rootScope.orgType);
            getPermittedMenuList(sourceMenuList, permittedMenuList);
            this.view.menuList = sourceMenuList;
          }
        });

      // 获取单位标段信息
      this.getUnitInfo = () => {
        $http
          .get(`/api/v1/org/get?id=${orgId}`)
          .then((res) => {
            if (res.success) {
              this.unitInfo = res.data;
            }
          });
      };

      this.getUnitInfo();
    },
    controllerAs: 'vm',
    link(scope, iElement) {
      iElement.on('click', 'a', (e) => {
        const $targetMenu = $(e.currentTarget);
        const $menuList = iElement.find('a');
        $menuList.removeClass('active');
        $targetMenu.addClass('active');
        if (!$targetMenu.hasClass('sub')) {
          scope.$apply(() => {
            const viewMenuList = scope.vm.view.menuList;
            if (viewMenuList && viewMenuList.length > 0) {
              viewMenuList.forEach((item) => {
                if ($targetMenu.text().trim() !== item.name) {
                  item.isExpanded = false;
                }
              });
            }
          });
        }
      });
    },
  };
}

export default sideNav;
